package cnhy.salary.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cnhy.salary.domain.proxy.AttendanceRecordProxy;
import com.easy.query.core.proxy.ProxyEntityAvailable;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.time.*;
import java.util.Date;
import lombok.Data;
import com.easy.query.core.annotation.*;
import ling.common.domain.entity.*;
import lombok.var;

/**
 * 考勤记录;
 * @author : He-LiangLiang@Outlook.com
 * @date : 2025-5-26
 */
@ApiModel(value = "考勤记录",description = "")
@EntityProxy
@Table("pay_attendance_record")
@Data
public class AttendanceRecord extends CreatedEntity.LongId implements IDeptEntity, Serializable,Cloneable, ProxyEntityAvailable<AttendanceRecord , AttendanceRecordProxy> {
     /**  */
    /*
    @Column(primaryKey = true)
    @ApiModelProperty(value = "",notes = "")
    private Long id ;
    */

     /** 打卡状态判断，首次打卡为空认为缺卡,并更新resultExt
      *
      */
     public void judgeResult(Date begin, Date end) {
          // 强制使用北京时间（UTC+8）
          ZoneId beijingZone = ZoneId.of("Asia/Shanghai");

          if (begin == null || end == null) {
               if (resultExt == null) {
                    resultExt = 0;
                    result = 0;
               }
               return;
          }

          // 转换为北京时间
          ZonedDateTime beginZdt = begin.toInstant().atZone(beijingZone);
          ZonedDateTime endZdt = end.toInstant().atZone(beijingZone);

          // 获取当天北京时间的日期部分
          ZonedDateTime dayDateZdt = getDayDate().toInstant().atZone(beijingZone);
          LocalDate currentDate = dayDateZdt.toLocalDate();

          // 组合成北京时间（当天日期+原始时间）
          ZonedDateTime beginDateTime = ZonedDateTime.of(
                  currentDate,
                  beginZdt.toLocalTime(),
                  beijingZone
          );
          ZonedDateTime endDateTime = ZonedDateTime.of(
                  currentDate,
                  endZdt.toLocalTime(),
                  beijingZone
          );

          // 处理跨天情况
          if (endDateTime.isBefore(beginDateTime)) {
               endDateTime = endDateTime.plusDays(1);
          }

          if (begin == null)
               return;

          // 转换为Date类型（兼容原有代码）
          Date beginDate = Date.from(beginDateTime.toInstant());
          Date endDate = Date.from(endDateTime.toInstant());

          // 考勤状态判断
          int re = 2; // 默认缺卡

          if (getFirstTime() == null) {
               re = 2;
          }
          else if (getFirstTime().after(beginDate)) {
               re = 1; // 迟到
          }
          else if (getLastTime() == null ||
                  Instant.now().isAfter(endDateTime.plusHours(2).toInstant())) {
               re = 2; // 缺卡
          }
          else {
               re = 0; // 正常
          }

          // 结果处理
          if (resultExt == null || resultExt < 3) {
               resultExt = re;
          }
          result = re;
     }
     @ApiModelProperty(value = "用户id", notes = "")
     @EasyWhereCondition(type = EasyWhereCondition.Condition.EQUAL)
     private Long userId;


     /**
      *
      */
     @Excel(name = "员工名称")
     @ApiModelProperty(value = "员工名称", notes = "")
     @EasyWhereCondition
     private String userName;

     @Excel(name = "考勤日期", dateFormat = "yyyy-MM-dd")
     @ApiModelProperty(value = "考勤日期", notes = "")
     private Date dayDate;
     /**
      * 首次打卡
      */
     @Excel(name = "首次打卡",dateFormat = "HH:mm:ss")
     @ApiModelProperty(value = "首次打卡", notes = "")
     private Date firstTime;


     /**
      * 末次打卡
      */
     @Excel(name = "末次打卡",dateFormat = "HH:mm:ss")
     @ApiModelProperty(value = "末次打卡", notes = "")
     private Date lastTime;


     /**
      * 0正常、1迟到、2缺卡
      */

     @ApiModelProperty(value = "0正常、1迟到、2缺卡", notes = "")
     private Integer result;


     /**
      * 0正常、1迟到、2缺卡,10补卡
      */
     @Excel(name = "考勤结果",readConverterExp = "0=正常,1=迟到,2=缺卡,10=补卡")
     @ApiModelProperty(value = "0正常、1迟到、2缺卡,10补卡", notes = "")
     @EasyWhereCondition(type = EasyWhereCondition.Condition.EQUAL)
     private Integer resultExt;


     /**
      * 记录来源标记
      */
     @ApiModelProperty(value = "记录来源标记", notes = "")
     private String source;

     @EasyWhereCondition(type = EasyWhereCondition.Condition.EQUAL)
     public Long deptId;
     String deptName;
     @EasyWhereCondition(type = EasyWhereCondition.Condition.EQUAL)
     Long postId;

     @ColumnIgnore
     private Long queryTime;

     /** 排序 */
     @ApiModelProperty(value = "排序",notes = "")
     private Integer orderNum;
 }
